package core.domain.query.paging;

import java.util.List;

import javax.persistence.Query;

import core.domain.IdEntity;

public class JpaQueryPagingCursor<T extends IdEntity> extends AbstractPagingCursor<T> {

    private Query dataQuery;

    private Query sizeQuery;

    public JpaQueryPagingCursor(Query dataQuery, Query sizeQuery, int pageSize) {
        super(pageSize);
        this.dataQuery = dataQuery;
        this.sizeQuery = sizeQuery;
    }

    @Override
    protected List<T> fetchContent() {
        dataQuery.setFirstResult(pageSize() * currentPage());
        dataQuery.setMaxResults(pageSize());
        return dataQuery.getResultList();
    }

    @Override
    protected int calcTotalSize() {
        return ((Number) sizeQuery.getSingleResult()).intValue();
    }

}
